﻿using System;
using System.Collections;
using System.Data;
using System.Reflection;
using System.Xml;

namespace ZYCommon
{
    

    public class XMLHttpReader : IDataReader, IDisposable, IDataRecord
    {
        private bool bolClosed = false;
        private const string c_NullFlag = "[NULL]";
        private int intRowIndex = 0;
        private ArrayList myRows = new ArrayList();
        private string[] strHead = null;

        public void Close()
        {
            this.bolClosed = true;
            this.strHead = null;
            this.myRows = null;
        }

        public void Dispose()
        {
        }

        public int FromReader(IDataReader myReader)
        {
            if (myReader != null)
            {
                this.strHead = new string[myReader.FieldCount];
                for (int i = 0; i < myReader.FieldCount; i++)
                {
                    this.strHead[i] = myReader.GetName(i).ToLower().Trim();
                }
                this.myRows.Clear();
                this.intRowIndex = -1;
                while (myReader.Read())
                {
                    string[] strArray = new string[myReader.FieldCount];
                    this.myRows.Add(strArray);
                    for (int j = 0; j < myReader.FieldCount; j++)
                    {
                        if (!myReader.IsDBNull(j))
                        {
                            if (myReader[j] is byte[])
                            {
                                strArray[j] = Convert.ToBase64String((byte[]) myReader[j]);
                            }
                            else
                            {
                                strArray[j] = myReader[j].ToString();
                            }
                        }
                    }
                }
                return this.myRows.Count;
            }
            return -1;
        }

        public int FromXML(XmlElement RootElement)
        {
            XmlElement element = null;
            this.strHead = null;
            this.myRows.Clear();
            this.intRowIndex = -1;
            if (RootElement == null)
            {
                return -1;
            }
            ArrayList list = new ArrayList();
            for (int i = 0; i < 0xff; i++)
            {
                if (!RootElement.HasAttribute("f" + i.ToString()))
                {
                    break;
                }
                string attribute = RootElement.GetAttribute("f" + i.ToString());
                list.Add(attribute.ToLower().Trim());
            }
            this.strHead = (string[]) list.ToArray(typeof(string));
            foreach (XmlNode node in RootElement.ChildNodes)
            {
                if (node is XmlElement)
                {
                    element = (XmlElement) node;
                    string[] strArray = new string[this.strHead.Length];
                    this.myRows.Add(strArray);
                    int index = 0;
                    foreach (XmlNode node2 in element.ChildNodes)
                    {
                        if (node2 is XmlElement)
                        {
                            strArray[index] = (node2.InnerText == "[NULL]") ? null : node2.InnerText;
                            index++;
                        }
                    }
                    continue;
                }
            }
            return this.myRows.Count;
        }

        public bool GetBoolean(int i)
        {
            return Convert.ToBoolean(this.GetString(i));
        }

        public byte GetByte(int i)
        {
            return Convert.ToByte(this.GetString(i));
        }

        public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
        {
            byte[] buffer2 = Convert.FromBase64String(this.GetString(i));
            int num = 0;
            while ((num < length) && ((num + fieldOffset) < buffer2.Length))
            {
                buffer[num + bufferoffset] = buffer2[(int) ((IntPtr) (num + fieldOffset))];
                num++;
            }
            return (long) num;
        }

        public char GetChar(int i)
        {
            return Convert.ToChar(this.GetString(i));
        }

        public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
        {
            return 0L;
        }

        public IDataReader GetData(int i)
        {
            return null;
        }

        public string GetDataTypeName(int i)
        {
            return null;
        }

        public DateTime GetDateTime(int i)
        {
            return Convert.ToDateTime(this.GetString(i));
        }

        public decimal GetDecimal(int i)
        {
            return Convert.ToDecimal(this.GetString(i));
        }

        public double GetDouble(int i)
        {
            return Convert.ToDouble(this.GetString(i));
        }

        public Type GetFieldType(int i)
        {
            return null;
        }

        public float GetFloat(int i)
        {
            return Convert.ToSingle(this.GetString(i));
        }

        public Guid GetGuid(int i)
        {
            return new Guid();
        }

        public int GetIndexByName(string strName)
        {
            if (strName != null)
            {
                strName = strName.ToUpper().Trim();
                for (int i = 0; i < this.strHead.Length; i++)
                {
                    if (strName.Equals(this.strHead[i]))
                    {
                        return i;
                    }
                }
            }
            return -1;
        }

        public short GetInt16(int i)
        {
            return Convert.ToInt16(this.GetString(i));
        }

        public int GetInt32(int i)
        {
            return Convert.ToInt32(this.GetString(i));
        }

        public long GetInt64(int i)
        {
            return Convert.ToInt64(this.GetString(i));
        }

        public string GetName(int i)
        {
            if ((i >= 0) && (i < this.strHead.Length))
            {
                return this.strHead[i];
            }
            return null;
        }

        public int GetOrdinal(string name)
        {
            return 0;
        }

        public DataTable GetSchemaTable()
        {
            return null;
        }

        public string GetString(int i)
        {
            if (((this.intRowIndex < 0) || (this.intRowIndex >= this.myRows.Count)) || ((i < 0) || (i >= this.strHead.Length)))
            {
                throw new IndexOutOfRangeException("记录表错误：行数或列数越界,Row:" + this.intRowIndex.ToString() + " Col:" + i.ToString());
            }
            string[] strArray = (string[]) this.myRows[this.intRowIndex];
            return strArray[i];
        }

        public object GetValue(int i)
        {
            return this.GetString(i);
        }

        public int GetValues(object[] values)
        {
            return 0;
        }

        public bool IsDBNull(int i)
        {
            return (this.GetString(i) == null);
        }

        public bool NextResult()
        {
            if ((this.myRows == null) || ((this.intRowIndex + 1) >= this.myRows.Count))
            {
                return false;
            }
            return true;
        }

        public bool Read()
        {
            this.intRowIndex++;
            if (this.intRowIndex >= this.myRows.Count)
            {
                return false;
            }
            return true;
        }

        public int ToXML(XmlElement RootElement)
        {
            XmlElement newChild = null;
            XmlElement element2 = null;
            while (RootElement.FirstChild != null)
            {
                RootElement.RemoveChild(RootElement.FirstChild);
            }
            if ((this.strHead != null) && (this.strHead.Length > 0))
            {
                for (int i = 0; i < this.strHead.Length; i++)
                {
                    RootElement.SetAttribute("f" + i.ToString(), this.strHead[i]);
                }
                RootElement.SetAttribute("fieldcount", this.strHead.Length.ToString());
            }
            if ((this.myRows != null) && (this.myRows.Count > 0))
            {
                XmlDocument ownerDocument = RootElement.OwnerDocument;
                for (int j = 0; j < this.myRows.Count; j++)
                {
                    string[] strArray = (string[]) this.myRows[j];
                    newChild = ownerDocument.CreateElement("r");
                    RootElement.AppendChild(newChild);
                    for (int k = 0; k < strArray.Length; k++)
                    {
                        element2 = ownerDocument.CreateElement("f" + k.ToString());
                        newChild.AppendChild(element2);
                        if (strArray[k] == null)
                        {
                            element2.InnerText = "[NULL]";
                        }
                        else
                        {
                            element2.InnerText = strArray[k];
                        }
                    }
                }
                return this.myRows.Count;
            }
            return -1;
        }

        public int Depth
        {
            get
            {
                return 0;
            }
        }

        public int FieldCount
        {
            get
            {
                return this.strHead.Length;
            }
        }

        public bool IsClosed
        {
            get
            {
                return this.bolClosed;
            }
        }

        public object this[string name]
        {
            get
            {
                return this.GetString(this.GetIndexByName(name));
            }
        }

        public int RecordsAffected
        {
            get
            {
                return 0;
            }
        }

        object IDataRecord.this[int i]
        {
            get
            {
                return this.GetString(i);
            }
        }
    }
}

